#include <windows.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define THREADNUM 3   //线程数
HANDLE Mutex;           

DWORD WINAPI ThreadFunc(LPVOID lpParam)
{
	time_t cur_time = time(NULL);  
	tm *t = localtime(&cur_time);
	WaitForSingleObject(Mutex,INFINITE);
	printf("%d-%02d-%02d %02d:%02d:%02d\n",
	t->tm_year + 1900,
	t->tm_mon + 1,
	t->tm_mday,
	t->tm_hour,
	t->tm_min,
	t->tm_sec);

	int thour = t->tm_hour;
	putchar(' ');
	if(thour<12)
	{
		putchar('A');
	}
	else
	{
		putchar('P');
	}
	putchar('M');
	putchar('\n');
	ReleaseMutex(Mutex);
    return 0;
}

int main(int argc, char* argv[])
{
    DWORD dwThreadId[THREADNUM]; 
    HANDLE hThread[THREADNUM]; 
	Mutex=CreateMutex(NULL,false,NULL);
	for(int i=0;i<THREADNUM;i++)
		hThread[i]= CreateThread( 
			NULL,
			0,
			ThreadFunc,
			NULL,
			CREATE_SUSPENDED,
			&dwThreadId[i]);
	for(int i=0;i<THREADNUM;i++)
		ResumeThread(hThread[i]);
    Sleep(500*THREADNUM);
    return 0;
}
